package com.ysw.service.impl;

import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * @author: yu_sw
 * @Description: com.ysw.service.impl
 * @date:2023/4/27 23:44
 */
public class TransactionSynchronizationUtil {
    public static void doAfterTransaction(Runnable r) {
        //判断是否开启事务
        if (TransactionSynchronizationManager.isActualTransactionActive()) {
            TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
                @Override
                public void afterCompletion(int status) {
                    if (status == TransactionSynchronization.STATUS_COMMITTED) {
                        r.run();
                    }
                }
            });
        }
    }
}
